package com.cwx.tools;

import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/29 0029.
 */
public class OKHttpUtils {

    public static Response post(String url, Map<String,String> headers, Map<String,String> params) throws IOException {
        FormBody.Builder bodyb = new FormBody.Builder();
        if (params != null) {
            for (String k : params.keySet()) {
                bodyb.add(k, params.get(k));
            }
        }
        FormBody body = bodyb.build();

        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String k : headers.keySet()) {
                builder = builder.header(k, headers.get(k));
            }
        }
        Request request = builder.post(body).build();

        return client.newCall(request).execute();
    }

    public static OkHttpClient client = new OkHttpClient()
            .newBuilder()
//            .followRedirects(false)
//            .followSslRedirects(false)
            .cookieJar(new CookieJar() {
                //CookieJar是用于保存Cookie的
                List<Cookie> cookies = null;

                @Override
                public List<Cookie> loadForRequest(HttpUrl arg0) {
                    if (cookies != null)
                        return cookies;
                    return new ArrayList<Cookie>();
                }

                @Override
                public void saveFromResponse(HttpUrl arg0, List<Cookie> arg1) {
                    for (Cookie c : arg1) {
                        cookies.add(c);
                    }
                }
            }).build();

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static Response post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    public static Response get(String url) throws IOException {
        Request req = new Request.Builder().url(url).get().build();
        Response response = client.newCall(req).execute();
        if (response.isSuccessful()) {
            return response;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
    public static Response get(String url, Map<String,String> headers) throws IOException {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String k : headers.keySet()) {
                builder = builder.header(k, headers.get(k));
            }
        }
        Request req = builder.get().build();
        Response response = client.newCall(req).execute();
        if (response.isSuccessful()) {
            return response;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
}
